
const baseApi = '/mockApi'

const tokens = {
  adm: {
    token: 'admin-token'
  },
  tea: {
    token: 'teacher-token'
  },
  stu: {
    token: 'student-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'admin001'
  },
  'teacher-token': {
    roles: ['teacher'],
    introduction: 'I am an teacher',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'teacher002'
  },
  'student-token': {
    roles: ['student'],
    introduction: 'I am an student',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'student003'
  }

}

module.exports = [
  // user login
  {
    url: `${baseApi}/user/login`,
    type: 'post',
    response: config => {
      const { username, captcha } = config.body
      if (captcha !== '0000') {
        return {
          code: 60203,
          message: '验证码错误'
        }
      }
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: '账号或密码错误'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },

  // get user info
  {
    url: `${baseApi}/user/info\.*`,
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: '无效的token，登录状态失效。'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: `${baseApi}/user/logout`,
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
